#include <vector>
#include <set>
#include <iostream>
using namespace std;

int mythirdMax(vector<int>& nums) {
    vector<long> three =  {LONG_MIN, LONG_MIN, LONG_MIN}; int flag = 0;
    for(int i = 0; i < nums.size(); i++){
        if(nums[i] > three[2]){
            three[0] = three[1];
            three[1] = three[2];
            three[2] = nums[i];
            flag++;
        }else if(nums[i] > three[1] && nums[i] < three[2]){
            three[0] = three[1];
            three[1] = nums[i];
            flag++;
        }else if(nums[i] > three[0] && nums[i] < three[1]){
            three[0] = nums[i];
            flag++;
        }
    }
    if(flag < 3){
        return three[2];
    }else{
        return three[0];
    }
}

int thirdMax(vector<int> &nums) {
    // set<int>类型是一种排好序的数组，存进去的元素可以自动排序，且每个元素出现有且仅有一次
    set<int> s;
    // 迭代器遍历，表示迭代nums的长度次，并且每次把nums[i]的值给num变量
    for (int num : nums) {
        s.insert(num);
        if (s.size() > 3) {
            s.erase(s.begin());
        }
    }
    return s.size() == 3 ? *s.begin() : *s.rbegin();
}

int main(){
    vector<int> nums = { 1,2,-2147483648 }; 
    cout << thirdMax(nums) <<endl;
    return 0;
}